#!/bin/sh

# This script runs when init it run during the boot process.
# Mounts everything in the fstab

mount -a
#mount -o remount +w /

#
# Mount the RAM filesystem to /tmp
#

mount -t ramfs -n none /tmp
mount -t ramfs -n none /var
mount -t sysfs sysfs /sys
if [ -f "/etc/rc.d/mount-fs.sh" ];then
	/etc/rc.d/mount-fs.sh
fi

export PATH=$PATH:/etc/ath
mkdir -p /var/run/srv
chmod 777 /var/run/srv
mkdir -p /var/run/client
chmod 777 /var/run/client
mkdir -p /tmp/log/error
mkdir -p /tmp/log/rec
mkdir -p /tmp/log/coredump
mkdir -p /tmp/logdump
chmod 777 /tmp/logdump

#used by fw_setenv
mkdir -p /var/lock
chmod 777 /var/lock

if [ -f "/sbin/disable_phy" ];then
echo "disable all phy"
/sbin/disable_phy
fi

if [ -f "/usr/bin/httpd_portal" ];then
echo "/usr/bin/httpd_portal -s run"
/usr/bin/httpd_portal -s &
fi

mkdir -p /var/run/lldpd
chmod 777 /var/run/lldpd

#ready for caldata/factory data, should be early
if [ -f "/sbin/mount_tpdata" ];then
/sbin/mount_tpdata
fi

#replace key
nvrammanager -n

nvrammanager -C

#mknod
if [ -f /etc/rc.d/mknod.sh ];then
/etc/rc.d/mknod.sh
fi
ulimit -c unlimited
echo "/tmp/log/coredump/core-%e-%p-%s-%t" > /proc/sys/kernel/core_pattern
#dd if=/dev/mtdblock6 of=/tmp/wifi0.caldata bs=32 count=377 skip=128
#dd if=/dev/mtdblock6 of=/tmp/wifi1.caldata bs=32 count=377 skip=640

if [ -f "/sbin/hotplug2" ];then
killall -q hotplug2
[ -x /sbin/hotplug2 ] && /sbin/hotplug2 --override --persistent \
                        --set-rules-file /etc/hotplug2.rules \
                        --set-coldplug-cmd /sbin/udevtrigger \
                        --max-children 1 >/dev/null 2>&1 &
fi

#
# Set lo up
#
ifconfig lo 127.0.0.1 up

#
# insert netfilter/iptables modules
#
if [ -f "/etc/nrd.conf" ];then
cp /etc/nrd.conf /tmp/nrd.conf
fi

if [ -f "/lib/firmware/AR900B/hw.2/boarddata_eu.bin" ];then
cp /lib/firmware/AR900B/hw.2/boarddata_eu.bin /tmp/boarddata_0.bin
cp /lib/firmware/AR900B/hw.2/boarddata_eu.bin /tmp/boarddata_1.bin
fi

/etc/rc.d/rc.modules

if [ -f /etc/rc.d/rc.ext ];then
/etc/rc.d/rc.ext
fi

#restore nvram setting, should call after wlcsm module init
if [ -f /etc/rc.d/hndnvram.sh ];then
/etc/rc.d/hndnvram.sh start
fi

if [ -f "/etc/rc.d/rc.ipq" ];then
/etc/rc.d/rc.ipq
fi

#
# Start Our AP Program
#
# generate rsa key will take a long time, so generate ras key firstly
if [ -f "/usr/bin/tdpcli" ];then
/usr/bin/tdpcli -g &
else
echo "tdpcli is not supported"
fi

if [ -f /usr/bin/poe ];then
/usr/bin/poe &
fi

#poe out 
# i2cset -y 0 0x20 0x03 0x0f
# i2cset -y 0 0x20 0x05 0x13
# i2cset -y 0 0x20 0x06 0x33
# i2cset -y 0 0x20 0x0f 0x06
# i2cset -y 0 0x20 0x13 0x02
# i2cset -y 0 0x20 0x14 0x02
/usr/bin/gpiod
/usr/bin/ntpd &
/sbin/udhcpc -p /tmp/udhcpc.pid -s /etc/udhcpc.script -a &

# ipv6
if [ -f "/usr/bin/dhcp6c" ];then
/usr/bin/dhcp6c -fd br0 &
else
echo "dhcp6c is not supported"
fi

/usr/bin/detect &
/usr/bin/msg-center &
/usr/bin/tpsyslogd &
/usr/bin/radius &
/usr/bin/auth-time &
/usr/bin/rssift &
/usr/bin/apscan &
/usr/bin/wscheduler &
/usr/bin/client-state &
if [ -f "/usr/bin/tp_bndstrg" ];then
/usr/bin/tp_bndstrg
fi
/usr/bin/cliserverd &

if [ -f "/usr/bin/speedTest" ];then
/usr/bin/speedTest &
else
echo "speedTest is not supported"
fi

if [ -f "/usr/bin/eap-mesh" ];then
/usr/bin/eap-mesh &
else
echo "mesh is not supported"
fi

if [ -f "/usr/bin/tdpcli" ];then
/usr/bin/tdpcli &
else
echo "tdpcli is not supported"
fi

if [ -f "/usr/bin/cluster-master" ];then
/usr/bin/cluster-master &
else
echo "cluster-master is not supported"
fi

if [ -f "/usr/bin/cluster-slave" ];then
/usr/bin/cluster-slave &
else
echo "cluster-slave is not supported"
fi

if [ -f "/usr/bin/cluster-cloud" ];then
/usr/bin/cluster-cloud &
else
echo "cluster-cloud is not supported"
fi

if [ -f "/usr/bin/cluster_elect" ];then
/usr/bin/cluster_elect &
else
echo "cluster_elect is not supported"
fi

if [ -f "/usr/bin/chan_optim" ];then
/usr/bin/chan_optim &
else
echo "chan_optim is not supported"
fi

/usr/bin/uclited &

if [ -f "/sbin/wlanmonitor" ];then
/sbin/wlanmonitor &
else
echo "wlanmonitor is not supported."
fi

echo 524288 > /proc/sys/net/ipv4/ipfrag_high_thresh

/etc/rc.d/rc.eapcs &

if [ -f "/usr/sbin/ap_watchdog" ];then
/usr/sbin/ap_watchdog &
else
echo "ap_watchdog is not supported."
fi

if [ -f "/usr/bin/channeldeploy" ];then
/usr/bin/channeldeploy &
else
echo "channeldeploy is not supported"
fi

if [ -f "/usr/bin/smartantenna" ];then
/usr/bin/smartantenna &
else
echo "smartantenna is not supported"
fi

if [ -f "/usr/bin/rfscan" ];then
/usr/bin/rfscan &
else
echo "rfscan is not supported"
fi

if [ -f "/usr/bin/netcheck" ];then
nvrammanager -r /tmp/cc -p region
if [ x`cat /tmp/cc|grep JP` != x ]; then
/usr/bin/netcheck &
else
echo "Japan disaster mode is not enabled for region not match"
fi
rm /tmp/cc
else
echo "Japan disaster mode is not supported"
fi

if [ -f "/usr/bin/troubleshooting" ];then
/usr/bin/troubleshooting &
else
echo "troubleshooting is not supported"
fi

if [ -f "/usr/bin/alignment" ];then
/usr/bin/alignment &
else
echo "alignment is not supported"
fi


